/**
 Ceated by Amit Mudgal (MicroStrategy Principal Designer) for GMS on 6/20/2014
 *
 */
window.uam = window.uam || {};
uam.Utils = {
		
	/* ************************
	 * Request Codes 
	 * ***********************/
	requestCode : {
		UP_REGISTRATION : 101,
		UP_EDIT : 102,
		UP_VIEW : 103
	},
	
	/* ************************
	 * Response Codes 
	 * ***********************/
	responseCode : {
		OK : 200,
		ERROR : 500,
		// invalid request code
		ERROR_INVALID_REQUEST_CODE : 501,
		ERROR_INVALID_PARAMETERS : 502
	},
	
	/**
	 * ajax call to task
	 * 
	 * @param params:Object request parameter list
	 * @param succcess:Function handle the callback
	 * 
	 */
	callTask:function(params, success){
		
		if(typeof(microstrategy) != 'undefined'){
			var session = String(microstrategy.sessionState);
		}
		
		params = params || {};
		
		params["taskId"] = params["taskId"] || 'UserProfileService';
		params["taskEnv"] = params["taskEnv"] || 'xhr';
        params["taskContentType"] = params["taskContentType"] || 'json';
        params["session"] = params["session"] || String(session);
        params["dateTime"] = params["dateTime"] || (new Date()).getTime();
        
        //formValueSubmit:
        
        success = success || function(data){ console ? console.log("callTask.data = " + data) : alert("callTask.data = " + data); }
		
        console.log(params);
        
		taskUrl = './taskProc';
		$.ajax({
			url:  taskUrl,
			type: 'GET',
			data: params,
			cache: "false",
			dataType: "text",
			async: false, //set to false in case we want it to wait
			success: success ,
			error: function (XMLHttpRequest, textStatus, errorThrown) {
		        alert("XMLHttpRequest="+XMLHttpRequest.responseText+"\ntextStatus="+textStatus+"\nerrorThrown="+errorThrown);
		    } 
		});
	}
};
$(document).ready(function(){
	//$("[readonly='readonly']").css({"background":"red"});
	/*alert("helo");
	var dataType = "xml";
	uam.Utils.callTask({'taskId':'N001', 'taskEnv':'xml','taskContentType':'xml',
		'TaskSubID':'FunctionalGroup','param1':'User'},,function(data){
			$(data).find('taskResponse').each(function(){
	    		 $(this).find('indexgrp').each(function(){
                 //alert($(this).text() );
                 var all = $(this).text().split(",");
                 var value = all[1];
                 var Group = all[0];
                 $("#funcGroupStep1").append('<option value=' +value + '>' + Group + '</option>');
	    	      })
	    	 })
	    	 alert("lllll");
	});*/
	
	
	taskUrl = './taskProc';
	data = {}
	$.ajax({
		url:  taskUrl,
		type: 'GET',
		data: {
		      taskId:'N001',
		      taskEnv:'xml',
		      taskContentType:'xml',
		      TaskSubID:'FunctionalGroup',
		      param1:'User'
		    },
		cache: "false",
		dataType: "xml",
		async: false, //set to false in case we want it to wait
		success: function(data){
			$(data).find('taskResponse').each(function(){
	    		 $(this).find('indexgrp').each(function(){
                  //alert($(this).text() );
                  var all = $(this).text().split(",");
                  var value = all[1];
                  var Group = all[0];
                  $("#funcGroupStep1").append('<option value=' +value + '>' + Group + '</option>');
	    	      })
	    	 })    
		
		} ,
		error: function (XMLHttpRequest, textStatus, errorThrown) {
	    }

	});
	
			
	if($.cookie("TransferData")!=null || $.cookie("TransferData")!="" || $.cookie("TransferData")!=" "){
		var alldata = $.cookie("TransferData").split("|");
	    var employeeNumber = alldata[0].replace("\"","");
	    var givenName = alldata[1];
	    var sn = alldata[2];
	    var axppmiddlename = alldata[3];
	    var axppdepartment = alldata[4];
	    var axppbusiness_cd = alldata[5];
	    var axppbusiness_descr = alldata[6];
	    var axppcccd = alldata[7];
	    var mail = alldata[8];
	    var axppmanagerid = alldata[9];
	    var axppunit_cd= alldata[10];
	    var axppunit_descr= alldata[11];
	    var axpwebid = alldata[12].replace("\"","");
	    if(axppmiddlename.replace("axppmiddlename:","")=="null"){
	    	var middleName = "";
	    }
	    if(axppbusiness_cd.replace("axppbusiness_cd:","")=="null"){
	    	var busnUDesc = "";
	    }
	    $('#preMstrAType').val("PA"); 
	    $('#preemail').val(mail.replace("mail: ",""));
	    $('#preempContID').val(employeeNumber.replace("employeenumber: ",""));
	    $('#preCostCenter').val(axppcccd.replace("axppcc-cd: ",""));
	    $('#preamexWebID').val(axpwebid.replace("uid: ",""));
	    $('#disabledPreFN').val(givenName.replace("givenName: ",""));
	    $('#disabledPreLN').val(sn.replace("sn: ",""));
	    $('#disabledPreMN').val(middleName);
	    $('#preAmexUC').val(axppunit_cd.replace("axppunit_cd: ",""));
	    $('#preAmexUDesc').val(axppunit_descr.replace("axppunit_descr: ",""));
	    $('#predepartmentId').val(axppdepartment.replace("axppdepartment: ",""));
	    $('#preDeprmntDesc').val(axppdepartment.replace("axppdepartment: ",""));
	    //$('#preBusnUC').val(axppbusiness_cd.replace("axppbusiness_cd:",""));
	    $('#preBusnUC').val(busnUDesc);
	    $('#preBusnUD').val(axppbusiness_descr.replace("axppbusiness_descr: ",""));
	}else{
		throw new Error("TransferData cookie is empty or unavailable");
	}
	

	taskUrl1 = './taskProc';
	data = {}
	
	$.ajax({
		url:  taskUrl1,
		type: 'GET',
		data: {
		      taskId:'N001',
		      taskEnv:'xml',
		      taskContentType:'xml',
		      TaskSubID:'FunctionalRole',
		      param1:'Role'
		    },
		cache: "false",
		dataType: "xml",
		async: false, //set to false in case we want it to wait
		success: function(data){
			    
				$(data).find('taskResponse').each(function(){
                  
	    		 $(this).find('indexrole').each(function(){
	    	     console.log("into funtcRoleStep2");
                 var fullData = $(this).text().split(",");
                 var val = fullData[1];
                 var Role = fullData[0];
                 var grpVal = fullData[2];
                 console.log("val::"+val);
                 console.log("Role::"+Role);
                 console.log("grpVal::"+grpVal);
                 $("#funtcRoleStep2").append('<option value=' + val + ' class='+ grpVal  + '>' + Role + '</option>');
	    	     })
	    	 })    
		
		} ,
		error: function (XMLHttpRequest, textStatus, errorThrown) { alert('Error');
	    }

	});
		
	
});


